/******************************************************************************

	COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2010.

	This file is part of Maxsi Engine.

	Maxsi Engine is free software: you can redistribute it and/or modify it
	under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or (at your
	option) any later version.

	Maxsi engine is distributed in the hope that it will be useful, but WITHOUT
	ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
	FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
	for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with Maxsi Engine. If not, see <http://www.gnu.org/licenses/>.

	MaxsiEngine.dll
	A general purpose C++ library for the Maxsi Technology Project.

	MaxsiTypes.cpp
	Functions for handling various common Maxsi Data Types.

******************************************************************************/

#include "MaxsiEngine.h"

BeginMaxsiNamespace

//=============================================================================
//	Index_t
//=============================================================================

const bool Index_t::IsSize()
{
	return ((Type & INDEX_T_USEDBITMASK) == INDEX_T_SIZE_T);
}

const bool Index_t::IsChar()
{
	return ((Type & INDEX_T_USEDBITMASK) == INDEX_T_CHAR);
}

const bool Index_t::IsWChar()
{
	return ((Type & INDEX_T_USEDBITMASK) == INDEX_T_WCHAR_T);
}

const bool Index_t::IsString()
{
	return (IsChar() || IsWChar());
}

size_t Index_t::IndexSize()
{
	if ( !IsSize() ) { return 0; }
	return (size_t)Index;
}

char* Index_t::IndexChar()
{
	if ( !IsChar() ) { return NULL; }
	return (char*)Index;
}

wchar_t* Index_t::IndexWChar()
{
	if ( !IsWChar() ) { return NULL; }
	return (wchar_t*)Index;
}

const bool Index_t::Compare(const size_t CompareTo)
{
	if ( IsSize() ) { return (size_t)Index == CompareTo; }

	return false;
}

const bool Index_t::Compare(const char* CompareTo)
{
	if ( IsChar() ) { return (MESTRCMP((char*)Index, CompareTo) == 0); }
	if ( IsWChar() ) { return (MESTRCMP((wchar_t*)Index, CompareTo) == 0); }

	return false;
}

const bool Index_t::Compare(const wchar_t* CompareTo)
{
	if ( IsChar() ) { return (MESTRCMP((char*)Index, CompareTo) == 0); }
	if ( IsWChar() ) { return (MESTRCMP((wchar_t*)Index, CompareTo) == 0); }

	return false;
}

Index_t& Index_t::operator=(const Index_t &rhs)
{
	Index	=	rhs.Index;
	Type	=	rhs.Type & INDEX_T_USEDBITMASK ;

	return *this;
}

Index_t& Index_t::operator=(const size_t &rhs)
{
	Index	=	(void*)rhs;
	Type	=	INDEX_T_SIZE_T;

	return *this;
}

Index_t& Index_t::operator=(const char* &rhs)
{
	Index	=	(void*)rhs;
	Type	=	INDEX_T_CHAR;

	return *this;
}

Index_t& Index_t::operator=(const wchar_t* &rhs)
{
	Index	=	(void*)rhs;
	Type	=	INDEX_T_WCHAR_T;

	return *this;
}

const bool Index_t::operator==(const Index_t &other)
{
	Index_t*	OtherP	=	(Index_t*)&other;

	if ( OtherP->IsSize() ) { return Compare(OtherP->IndexSize()); }
	if ( OtherP->IsChar() ) { return Compare(OtherP->IndexChar()); }
	if ( OtherP->IsWChar() ) { return Compare(OtherP->IndexWChar()); }

	return false;
}

const bool Index_t::operator==(const size_t &other)
{
	return Compare(other);
}
const bool Index_t::operator==(const char* &other)
{
	return Compare(other);
}
const bool Index_t::operator==(const wchar_t* &other)
{
	return Compare(other);
}

const bool Index_t::operator!=(const Index_t &other)
{
	Index_t*	OtherP	=	(Index_t*)&other;

	if ( OtherP->IsSize() ) { return !Compare(OtherP->IndexSize()); }
	if ( OtherP->IsChar() ) { return !Compare(OtherP->IndexChar()); }
	if ( OtherP->IsWChar() ) { return !Compare(OtherP->IndexWChar()); }
	return false;
}

const bool Index_t::operator!=(const size_t &other)
{
	return !Compare(other);
}

const bool Index_t::operator!=(const char* &other)
{
	return !Compare(other);
}

const bool Index_t::operator!=(const wchar_t* &other)
{
	return !Compare(other);
}

EndMaxsiNamespace
